/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dyniablaster.graphics;

import dyniablaster.logic.DyniaManager;
import java.io.IOException;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.GameCanvas;

/**
 * @author Damian2
 */
public class DyniaBlasterCanvas extends GameCanvas implements CommandListener {

    Display dyniaDisplay;
    DyniaBlasterVisualMIDlet dyniaMidlet;
    DyniaManager dyniaManager;

    int DISP_WIDTH;
    int DISP_HEIGHT;
    int CORNER_X;
    int CORNER_Y;

    boolean reallyQuitScreen;
    public boolean dyniaGameOver;
    public boolean dyniaPauseGame;

    /**
     * color constant
     */
    public static final int BLACK = 0;
    /**
     * color constant
     */
    public static final int WHITE = 0xffffffff;
    /**
     * color constant
     */
    public static final int OPAQUE_BLACK = 0xff000000;
    /**
     * color constant
     */
    public static final int OPAQUE_BLUE = 0xff0000ff;

    Command dyniaExit, dyniaPause, dyniaTak, dyniaNie, dyniaResume;

    String text;


    /**
     * constructor
     */
    public DyniaBlasterCanvas(DyniaBlasterVisualMIDlet midlet) 
    {
        super(false);
        try
        {
	    // Set up this canvas to listen to command events
	    
	    // Add the Exit command
        dyniaExit = new Command("Koniec", Command.ITEM, 1);
        dyniaPause = new Command("Pauza", Command.ITEM, 1);
        dyniaTak = new Command("Tak", Command.ITEM, 1);
        dyniaNie = new Command("Nie", Command.ITEM, 1);
        dyniaResume = new Command("Wznów", Command.ITEM, 1);

        addCommand(dyniaPause);
	    addCommand(dyniaExit);
        setCommandListener(this);

       //setTicker()
        } 
        catch(Exception e)
        {
            e.printStackTrace();
        }
        text = new String("test");
        reallyQuitScreen = false;
        dyniaGameOver = false;
        dyniaPauseGame = false;

        dyniaDisplay = Display.getDisplay(midlet);
        dyniaMidlet = midlet;
        //setFullScreenMode(true);

        DISP_WIDTH = getWidth();
        DISP_HEIGHT = getHeight();

        if (dyniaManager == null)
        {
            dyniaManager = new DyniaManager(CORNER_X, CORNER_Y, DISP_WIDTH, DISP_HEIGHT, this);
        }
    }

    public DyniaManager getManager()
    {
        return this.dyniaManager;
    }

    public void start() throws IOException
    {
        // Now that the timer and softkeys are ready,
        // this screen can be displayed (since the menu is
        // not shown initially)
        dyniaDisplay.setCurrent(this);

    }
    
    /**
     * paint
     */
    public void paint(Graphics g)
    {
         try 
         {
            dyniaManager.paint(g);
         }
         catch (Exception e) 
         {
            //myDungeon.errorMsg(e);
            return;
        }
        if(reallyQuitScreen)
        {
            g.setColor(OPAQUE_BLUE);
            g.fillRect(DISP_WIDTH/2 - DISP_WIDTH/3, DISP_HEIGHT/2 - DISP_HEIGHT/4, DISP_WIDTH*2/3, DISP_HEIGHT/2);
            g.setColor(WHITE);
            g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE));
            g.drawString("Czy napewno ", DISP_WIDTH/2, DISP_HEIGHT/2 - 20, Graphics.HCENTER | Graphics.BOTTOM);
            g.drawString("chcesz zakończyć ", DISP_WIDTH/2, DISP_HEIGHT/2 - 20 + Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE).getHeight(), Graphics.HCENTER | Graphics.BOTTOM);
            g.drawString("gre?", DISP_WIDTH/2, DISP_HEIGHT/2 - 20 + Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE).getHeight() + + Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE).getHeight(), Graphics.HCENTER | Graphics.BOTTOM);
            g.setColor(0);
            /*g.fillRect(0, getHeight()-20, getWidth(), getHeight());
            g.setColor(0xffffffff);
            g.drawString("Nie", 0, getHeight()-20, Graphics.TOP | Graphics.LEFT);
            g.drawString("Tak", getWidth()-20, getHeight()-20, Graphics.TOP | Graphics.RIGHT);*/
        }
        else if(dyniaPauseGame)
        {
           g.setColor(OPAQUE_BLUE);
            g.fillRect(DISP_WIDTH/2 - DISP_WIDTH/3, DISP_HEIGHT/2 - DISP_HEIGHT/4, DISP_WIDTH*2/3, DISP_HEIGHT/2);
            g.setColor(WHITE);
            g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE));
            g.drawString("PAUZA", DISP_WIDTH/2, DISP_HEIGHT/2 - 20, Graphics.HCENTER | Graphics.BOTTOM);
            g.setColor(0);
        }

        //g.drawString("Sample Text",0,0,Graphics.HCENTER|Graphics.LEFT);
    }

    public void updateGame(int dt)
    {
     updateScreen();
     dyniaManager.update(dt);
    }

    public void updateScreen()
    {
        // paint the display:
        /*if(!dyniaPauseGame)
            {*/
            try
            {
                paint(getGraphics());
                flushGraphics(CORNER_X, CORNER_Y, DISP_WIDTH, DISP_HEIGHT);
            }
            catch (Exception e)
            {
            //myDungeon.errorMsg(e);
            }
       // }
        //myGameTicks++;
    }

    public void checkKeys()
    {
        if (!dyniaPauseGame)
        {
            // determine which moves the user would like to make:
            int keyState = getKeyStates();
            int vertical = 0;
            int horizontal = 0;

            if((keyState & FIRE_PRESSED) != 0)
            {
                try {
                    dyniaManager.bomb();
                }
                catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
            else if ((keyState & LEFT_PRESSED) != 0)
            {
                horizontal = -1;

            }
            else if ((keyState & RIGHT_PRESSED) != 0)
            {
                horizontal = 1;
            }
            else if ((keyState & UP_PRESSED) != 0)
            {
                vertical = -1;
            }
            else if ((keyState & DOWN_PRESSED) != 0)
            {
                vertical = 1;
            }
            else if((keyState & FIRE_PRESSED) != 0)
            {
                try {
                    dyniaManager.bomb();
                }
                catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
           /* else
            {
                vertical = 0;
                horizontal = 0;
            }*/
            // tell the manager to move the duke
            // accordingly if possible:
            dyniaManager.requestMove(horizontal, vertical);
        }
    }
    
    /**
     * Called when a key is pressed.
     */
    protected  void keyPressed(int keyCode) {
    }
    
    /**
     * Called when a key is released.
     */
    protected  void keyReleased(int keyCode) {
    }

    /**
     * Called when a key is repeated (held down).
     */
    protected  void keyRepeated(int keyCode) {
    }
    
    /**
     * Called when the pointer is dragged.
     */
    protected  void pointerDragged(int x, int y) {
    }

    /**
     * Called when the pointer is pressed.
     */
    protected  void pointerPressed(int x, int y) {
    }

    /**
     * Called when the pointer is released.
     */
    protected  void pointerReleased(int x, int y) {
    }
    
    /**
     * Called when action should be handled
     */
    public void setGameOver()
    {
        dyniaGameOver = true;
    }
    public void commandAction(Command command, Displayable displayable)
    {
        if(command == dyniaExit)
        {
            reallyQuitScreen = true;
            removeCommand(dyniaExit);
            removeCommand(dyniaPause);
            if(dyniaPauseGame)
                removeCommand(dyniaResume);

            dyniaPauseGame = true;
            addCommand(dyniaNie);
            addCommand(dyniaTak);
        }
        else if(command  == dyniaPause)
        {
            dyniaPauseGame = true;
            removeCommand(dyniaExit);
            removeCommand(dyniaPause);
            addCommand(dyniaResume);
            addCommand(dyniaExit);
        }
        else if(command == dyniaResume)
        {
            dyniaPauseGame = false;
            removeCommand(dyniaResume);
            removeCommand(dyniaExit);
            addCommand(dyniaPause);
            addCommand(dyniaExit);
        }
        else if(command == dyniaTak)
        {
            if(reallyQuitScreen)
                dyniaMidlet.exitMIDlet();
        }
        else if(command == dyniaNie)
        {
            removeCommand(dyniaTak);
            removeCommand(dyniaNie);
            addCommand(dyniaPause);
            addCommand(dyniaExit);
            reallyQuitScreen = false;
            dyniaPauseGame = false;
        }

    }

}